package com.esri.android.map.bing;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.esri.android.map.TiledServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.a;
import com.esri.core.internal.io.handler.ResponseListener;
import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.bing.Response;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes.dex */
public class BingMapsLayer extends TiledServiceLayer {
    public static final double BING_MAPS_XMAX = 2.003750834278E7d;
    public static final double BING_MAPS_XMIN = -2.003750834278E7d;
    public static final double BING_MAPS_YMAX = 2.0037508342787E7d;
    public static final double BING_MAPS_YMIN = -2.003750834278E7d;
    private static Bitmap d;
    private static ByteArrayInputStream e;
    private static final double[] f = {78271.5169639999d, 39135.7584820001d, 19567.8792409999d, 9783.93962049996d, 4891.96981024998d, 2445.98490512499d, 1222.99245256249d, 611.49622628138d, 305.748113140558d, 152.874056570411d, 76.4370282850732d, 38.2185141425366d, 19.1092570712683d, 9.55462853563415d, 4.77731426794937d, 2.38865713397468d, 1.19432856685505d, 0.597164283559817d, 0.298582141647617d, 0.149291070823808d, 0.074645535411904d, 0.037322767705952d, 0.018661383985268d};
    private static final double[] g = {2.95828763795777E8d, 1.47914381897889E8d, 7.3957190948944E7d, 3.6978595474472E7d, 1.8489297737236E7d, 9244648.868618d, 4622324.434309d, 2311162.217155d, 1155581.108577d, 577790.554289d, 288895.277144d, 144447.638572d, 72223.819286d, 36111.909643d, 18055.954822d, 9027.977411d, 4513.988705d, 2256.994353d, 1128.497176d, 564.248588d, 282.124294d, 141.062147d, 70.531074d};
    private static String s = "V1";
    private static String t = "http://dev.virtualearth.net/REST/" + s + "/Imagery/Metadata/";
    ResponseListener c;
    private MapStyle u;
    private String v;
    private String w;
    private String x;
    private BingMapsResult y;
    private URL z;

    @Override // com.esri.android.map.Layer
    public void reinitializeLayer(UserCredentials userCredentials) {
    }

    /* loaded from: classes.dex */
    public enum MapStyle {
        AERIAL("Aerial"),
        AERIAL_WITH_LABELS("AerialWithLabels"),
        ROAD("Road");
        
        String a;

        MapStyle(String str) {
            this.a = str;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this.a;
        }

        public static MapStyle fromString(String str) {
            if ("Aerial".equals(str)) {
                return AERIAL;
            }
            if ("AerialWithLabels".equals(str)) {
                return AERIAL_WITH_LABELS;
            }
            if ("Road".equals(str)) {
                return ROAD;
            }
            return null;
        }
    }

    public URL getLogoUrl() {
        return this.z;
    }

    public BingMapsLayer(String str, MapStyle mapStyle) {
        this(str, mapStyle, true);
    }

    public BingMapsLayer(String str, MapStyle mapStyle, boolean z) {
        super(t);
        this.u = MapStyle.ROAD;
        this.v = "v1";
        this.w = "en-US";
        this.x = null;
        this.c = new ResponseListener() { // from class: com.esri.android.map.bing.BingMapsLayer.2
            @Override // com.esri.core.internal.io.handler.ResponseListener
            public boolean onResponseInterception(HttpResponse httpResponse) {
                Header[] headers = httpResponse.getHeaders("X-VE-Tile-Info");
                if (headers == null || headers.length <= 0) {
                    return false;
                }
                return "no-tile".equalsIgnoreCase(headers[0].getValue());
            }
        };
        this.x = str;
        this.u = mapStyle;
        this.w = Locale.getDefault().toString().replace('_', '-');
        this.isBingMap = true;
        if (z) {
            try {
                getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.bing.BingMapsLayer.1
                    @Override // java.lang.Runnable
                    public void run() {
                        BingMapsLayer.this.initLayer();
                    }
                });
            } catch (RejectedExecutionException e2) {
                Log.e(a.a, "initialization of the layer failed.", e2);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.TiledServiceLayer, com.esri.android.map.Layer
    public void initLayer() {
        if (getID() == 0) {
            this.nativeHandle = create();
        }
        if (getID() == 0) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            Log.e(a.a, "url =" + getUrl());
            return;
        }
        try {
            c();
            setDefaultSpatialReference(SpatialReference.create(102100));
            setFullExtent(new Envelope(-2.003750834278E7d, -2.003750834278E7d, 2.003750834278E7d, 2.0037508342787E7d));
            Point point = new Point(-2.003750834278E7d, 2.0037508342787E7d);
            double[] dArr = g;
            setTileInfo(new TiledServiceLayer.TileInfo(point, dArr, f, dArr.length, 96, this.y.getImageWidth(), this.y.getImageHeight()));
            super.initLayer();
        } catch (Exception e2) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_BING_LAYER));
            Log.e(a.a, "Bing map url =" + getUrl(), e2);
        }
    }

    void c() throws Exception {
        JsonParser a = h.a(t + this.u.toString(), d(), (n) null);
        Response response = new Response(BingMapsResult.class);
        response.fromJson(a);
        if (response.getResults() == null || response.getResults().isEmpty()) {
            throw new Exception("No Bing Maps information retrieved, unable to create the layer.");
        }
        BingMapsResult bingMapsResult = (BingMapsResult) response.getResults().get(0);
        this.y = bingMapsResult;
        setUrl(bingMapsResult.getImageUrl());
        this.z = response.getBrandLogoUri();
        e();
    }

    private Map<String, String> d() throws Exception {
        String str = this.x;
        if (str == null || str.trim().length() == 0) {
            throw new Exception("No Bing Maps application ID defined, unable to create the layer.");
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("mv", this.v);
        linkedHashMap.put("key", this.x);
        return linkedHashMap;
    }

    @Override // com.esri.android.map.TiledServiceLayer
    protected byte[] getTile(int i, int i2, int i3) throws Exception {
        String replaceFirst = this.y.getImageUrl().replaceFirst("\\{culture\\}", this.w);
        if (this.y.getImageUrlSubdomains() == null || this.y.getImageUrlSubdomains().length == 0) {
            throw new Exception("No subdomains have been retrieved: the tile URL cannot be constituted!");
        }
        String replaceFirst2 = replaceFirst.replaceFirst("\\{subdomain\\}", this.y.getImageUrlSubdomains()[i3 % this.y.getImageUrlSubdomains().length]);
        String str = "";
        for (int i4 = i + 1; i4 > 0; i4--) {
            int i5 = 1 << (i4 - 1);
            int i6 = (i2 & i5) != 0 ? 1 : 0;
            if ((i5 & i3) != 0) {
                i6 = i6 + 1 + 1;
            }
            str = str + i6;
        }
        return com.esri.core.internal.io.handler.a.a(replaceFirst2.replaceFirst("\\{quadkey\\}", str), (Map<String, String>) null, (n) null, this.c);
    }

    public MapStyle getMapStyle() {
        return this.u;
    }

    public void setMapStyle(MapStyle mapStyle) {
        this.u = mapStyle;
    }

    public String getMapVersion() {
        return this.v;
    }

    public void setMapVersion(String str) {
        this.v = str;
    }

    public String getCulture() {
        return this.w;
    }

    public void setCulture(String str) {
        if (str == null || str.length() <= 1) {
            return;
        }
        this.w = str;
    }

    public String getAppId() {
        return this.x;
    }

    public void setAppId(String str) {
        this.x = str;
    }

    public void refresh() {
        try {
            getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.bing.BingMapsLayer.3
                @Override // java.lang.Runnable
                public void run() {
                    if (BingMapsLayer.this.isInitialized()) {
                        try {
                            BingMapsLayer.this.c();
                            BingMapsLayer.this.clearTiles();
                        } catch (Exception e2) {
                            Log.e(a.a, "Re-initialization of the layer failed.", e2);
                        }
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public Bitmap getBingLogo(int i, int i2) {
        if (d == null && e != null && i > 1 && i2 > 1) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.outHeight = i2;
            options.outWidth = i;
            options.inPurgeable = true;
            d = BitmapFactory.decodeStream(e, null, options);
            try {
                e.close();
            } catch (IOException e2) {
                Log.e(a.a, "Error while trying to close.", e2);
            }
            e = null;
        }
        return d;
    }

    public void reinitializeLayer(String str) {
        this.x = str;
        super.reinitializeLayer((UserCredentials) null);
    }

    private void e() {
        InputStream inputStream = null;
        try {
            try {
                try {
                    if (getLogoUrl() != null) {
                        inputStream = getLogoUrl().openStream();
                        byte[] bArr = new byte[1024];
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        while (true) {
                            int read = inputStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            byteArrayOutputStream.write(bArr, 0, read);
                        }
                        e = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (Throwable th) {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e2) {
                            Log.e(a.a, "Error while trying to load the Bing logo.", e2);
                        }
                    }
                    throw th;
                }
            } catch (IOException e3) {
                Log.e(a.a, "Error while trying to load the Bing logo.", e3);
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        } catch (IOException e4) {
            Log.e(a.a, "Error while trying to load the Bing logo.", e4);
        }
    }
}
